package de.tsajar.es.input;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.simsilica.es.Entity;
import com.simsilica.es.EntitySet;
import de.tsajar.es.EntitySystemAppState;
import de.tsajar.es.character.JumpComponent;
import de.tsajar.es.character.WalkDirectionComponent;
import de.tsajar.es.input.devices.InputDevice;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Eike Foede
 */
public class DeviceInputSystem extends EntitySystemAppState {

    private EntitySet es;
    private Map<Integer, InputDevice> devices = new HashMap<Integer, InputDevice>();

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        es = ed.getEntities(PlayerInputComponent.class);
    }

    public void registerDevice(int deviceId, InputDevice device) {
        devices.put(deviceId, device);
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
        for (Entity e : es) {
            ed.setComponent(e.getId(), new WalkDirectionComponent(devices.get(e.get(PlayerInputComponent.class).getInputId()).getWalkDirection()));
            if (devices.get(e.get(PlayerInputComponent.class).getInputId()).getJumping()) {
                ed.setComponent(e.getId(), new JumpComponent());
            }
        }
    }
}
